package vn.student.Adapter;

import static vn.student.database.DBComman.CONTENT;
import static vn.student.database.DBComman.TITLE;
import static vn.student.util.CusorUtil.getCusorStr;

import java.util.regex.Pattern;

import vn.student.notificationforandroid.R;
import android.content.Context;
import android.database.Cursor;
import android.text.Html;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterThongBao extends SimpleCursorAdapter {
    LayoutInflater inflater;
    Cursor c;
    int layout;
    Context context;

    public AdapterThongBao(Context context, int layout, Cursor c,
	    String[] from, int[] to) {
	super(context, layout, c, from, to);
	this.context=context;
	inflater = LayoutInflater.from(context);
	this.c = c;
	this.layout = layout;
    }

    public void setC(Cursor c) {
	this.c = c;
	this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	View v = convertView;
	c.moveToPosition(position);
	LayoutHorder layoutHorder;
	if (v == null) {
	    v = inflater.inflate(layout, null);
	    layoutHorder = new LayoutHorder();
	    layoutHorder.mTxtConent = (TextView) v
		    .findViewById(R.id.row_thongbao_content);
	    layoutHorder.mTxtTile = (TextView) v
		    .findViewById(R.id.row_thongbao_title);

	    v.setTag(layoutHorder);
	} else {
	    layoutHorder = (LayoutHorder) v.getTag();
	}
	Pattern pattern = Pattern.compile(".\\..");
	Linkify.addLinks(layoutHorder.mTxtConent, pattern, "http://");
	layoutHorder.mTxtConent.setText(Html.fromHtml(getCusorStr(c, CONTENT)));
	layoutHorder.mTxtTile.setText(getCusorStr(c, TITLE));
	layoutHorder.mTxtConent.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			Toast.makeText(context, "Clciked", Toast.LENGTH_LONG).show();
		}
	});
	return v;
    }

    static class LayoutHorder {
	TextView mTxtTile, mTxtConent;
    }

}
